<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content=" initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <title>编辑发票</title>
    <link href="{$Think.config.RESOURCEURL}wx_assets/css/product_list.css?v=201408101" rel="stylesheet">
    <link href="{$Think.config.RESOURCEURL}wx_assets/css/product_templates.css?v=201401121" rel="stylesheet">
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/alertMsg.js"></script>
    <style>
        .divTitle{
            width:auto;
        }
        .divRad{
            margin-left:20px;
        }
        .mod_input {
            -webkit-appearance: none;
            display: none;
            padding-left: 3px;
            width: 95%;
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            border: 0;
            background: #fdfdfd;
            box-shadow: 1px 1px 1px rgba(0,0,0,.15) inset
        }
    </style>
</head>
<body>
<div class="lay_page">
    <div class="lay_page_wrap">
        <div class="qb_gap qb_pt10">
	       	<div>
	            <input type="hidden" id="invoiceId" value="{$invoiceId}">
	            <div>
	                <a class="divTitle">发票方式：</a>
	            </div>
	            <div class="divRad">
	                <volist name="invoiceMethod" id="invoiceMethod">
	               		<if condition ="$invoice['methodId'] eq $invoiceMethod['id']">
	                  	  <input type="radio" value="{$invoiceMethod.id}" name="invoiceMethod" checked />
	                    <else />
	                      <input type="radio" value="{$invoiceMethod.id}" name="invoiceMethod" />
	                    </if>{$invoiceMethod.name}<br/>
	                </volist>
	            </div>
	        </div>
	        <div>
	            <div>
	                <a class="divTitle">发票抬头：</a>
	            </div>
	            <div class="divRad">
	                <nobr>
	                	<if condition ="$invoice['title'] eq '1'">
	                    	<input type="radio" value="1" name="invTitle" checked>公司</input>
	                    	<input type="text" class="mod_input" id="invTitCom" style="display:inline-block;" value="{$invoice.titleName}"/>
	                    <else />
	                    	<input type="radio" value="1" name="invTitle">公司</input>
	                    	<input type="text" class="mod_input"  style="display:hidden;" id="invTitCom" value="{$invoice.titleName}"/>
	                    </if>
	                </nobr>
	                <br/>
	                 <if condition ="$invoice['title'] eq '1'">
	               	 	<input type="radio" value="2" name="invTitle">个人</input>
	               	 <else />
	               	 	<input type="radio" value="2" name="invTitle" checked>个人</input>
	               	 </if>
	            </div>
	        </div>
	        <div>
	            <div>
	                <a class="divTitle">发票明细：</a>
	            </div>
	            <div class="divRad">
	                <volist name="invoiceContent" id="invoiceContent">
	                	<if condition ="$invoice['contentTypeId'] eq $invoiceContent['id']">
	                   		 <input type="radio" value="{$invoiceContent.id}" name="invoiceContent" checked/>
	                    <else />
	                    	 <input type="radio" value="{$invoiceContent.id}" name="invoiceContent"/>
	                    </if>
	                    {$invoiceContent.name}<br/>
	                </volist>
	            </div>
	        </div>
	        <div class="qb_mb10" style="padding-top: 20px;">
	    		<a class="mod_btn btn_em btn_block" onclick="onSaveInvoice(this)">保存</a>
			</div>
	    </div>
    </div>
</div>

</body>

<script type="text/javascript" src="wx_assets/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="wx_assets/js/share_product.js?v=20140105"></script>
<script>
    var hrefParamsArray = GetSessionIdFromHref(window.location.search);
    hrefParamsArray["userName"]='{$userName}';
    function GetSessionIdFromHref(search){
        var args = search.substring(1);
        var retval = new Array();
        args = args.split("&");
        for(var i = 0; i < args.length; i ++){
            str = args[i];
            var arg = str.split("=");
            if(arg.length <= 1) continue;

            if(arg[0] == 'params') {
                var params = decodeURIComponent(arg[1]);
                var obj = eval("(" + params + ")");
                if (obj.rootid != undefined) {
                    retval["rootid"] = obj.rootid;
                }
                if (obj.id != undefined) {
                    retval["id"] = obj.id;
                }
                if (obj.tplid != undefined) {
                    retval["tplid"] = obj.tplid;
                }
                if (obj.ptype != undefined) {
                    retval["ptype"] = obj.ptype;
                }
                if (obj.dtype != undefined) {
                    retval["dtype"] = obj.dtype;
                }
                if(obj.invoice != undefined){
                    retval['invoice'] = obj.invoice;
                }
            } else if(arg[0] == 'customerId') {
                retval["customerId"] = arg[1];
            }else if(arg[0] == 'userName'){
                retval["userName"] = arg[1];
            }
        }
        return retval;
    }

    var hrefPath = 'call.html?customerId='+ hrefParamsArray["customerId"];
    var basePath = 'call.json?customerId='+ hrefParamsArray["customerId"]+'&userName='+hrefParamsArray["userName"];
    var rootId = hrefParamsArray["rootid"] == undefined ? '' : ',"rootid":' + hrefParamsArray["rootid"];
    var id =  hrefParamsArray["id"] == undefined ? '' : hrefParamsArray["id"];
    var tplid = hrefParamsArray["tplid"] == undefined ? 0 : hrefParamsArray["tplid"];
    var ptype = hrefParamsArray["ptype"]  == undefined ? '' : ',"ptype":"' + hrefParamsArray["ptype"] + '"';
    var dtype = hrefParamsArray["dtype"]  == undefined ? '' : ',"dtype":"' + hrefParamsArray["dtype"] + '"';
//    getInvoice();
//    function getInvoice(){
//        var b = {
//            'module': 'product',
//            'method': 'getOrderDetail',
//            'params': '{"tplid":1,"id":"'+id+'","ptype":"delivery","dtype":"delivery1"}'
//        };
//        $.ajax({
//            url: window.basePath,
//            dataType: "json",
//            type: "POST",
//            data: b,
//            success: function(data) {
//                if(data.code) {
//                    alert(data.msg);
//                } else {
//                    console.log(hrefParamsArray['invoice']);
//                    console.log(data.invoiceList);
//                }
//            }
//        });
//    }
    function onSaveInvoice(ctrl){
        var title = $("input[type='radio'][name='invTitle']:checked").val();
        var titleVal = $('#invTitCom').val();
        var invoiceContent = $("input[type='radio'][name='invoiceContent']:checked").val();
        var invoiceMethod = $("input[type='radio'][name='invoiceMethod']:checked").val();
        var invoiceId = $('#invoiceId').val().trim();
        if(!invoiceMethod || invoiceMethod == ''){
            alert('请选择发票方式!');
            return;
        }

        if(!title || title == ''){
            alert('请选择发票抬头!');
            return;
        }
        if(title == '1' && titleVal == ''){
            alert('请输入公司名称!');
            return;
        }

        if(!invoiceContent || invoiceContent == ''){
            alert('请选择发票内容!');
            return;
        }
        
        var method =  '{$method}';
        var invoiceIdParam = '';
        if(method == 'update' && invoiceId != '-1'){
            invoiceIdParam = ',"invoiceId": ' + invoiceId;
        }
        var b = {
        	'module': 'mall',
    	    'method': method + 'Invoice',
    	    'params': '{"title":"' + title + '","titleName":"' + titleVal + '","contentTypeId":"' + invoiceContent + 
    	    	'","methodId":"' + invoiceMethod  + '"' + invoiceIdParam +  '}'
        };
        $.ajax({
        	url: window.basePath,
 	        dataType: "json",
 	        type: "POST",
 	        data: b,
			success: function(data) {
				if(data.code) {
					  alert(data.msg);
				} else {
					var params;

	    	    	if (window.id == undefined) {
	    	    		params = '&module=product&method=confirmOrder&params={"type":"buy","invoiceId": ' + invoiceId+'"sid":-1' +
	    	    			window.rootId + window.ptype + window.dtype + '}';
	    	    	} else {
	    	    		params = '&module=product&method=getOrderDetail&params={"tplid":' + window.tplid + 
	    	    			',"invoiceId": ' + invoiceId+',"id":"' + window.id +'"'+  window.rootId + window.ptype +  window.dtype + '}';
	    	    	}
	                location.href = window.hrefPath + params;
				}
			}
		});
    }
</script>
<script>
    $("input[name='invTitle']").change(function(){
        if($(this).val()==1){
            $("#invTitCom").show();
        }else{
            $("#invTitCom").hide();
        }
    });

    $("input[name='invDetailed']").change(function(){
        if($(this).val()==1){
            $("#invDetail").show();
        }else{
            $("#invDetail").hide();
        }
    });
</script>
</html>